(function($){
	$.extend($.fn,{
		dateBuilder:function(options){
			var opts=$.extend($.fn.dateBuilder.Defaults,options);
			if(opts.month>11||opts.month<0){
				alert("请检查日期输入格式！");
				return;
			}
			var db=$("table.dataBuilder"),dbItem=db.find("td");
			if(db.length<1){
				var dbHtml='<table class="dataBuilder">'
							+'<caption><span class="after">></span><span class="before"><</span><span class="date"><i class="year" id="dbYear">'+opts.year+'</i>年<i class="month" id="dbMonth">'+(parseInt(opts.month)+1)+'</i>月</span></caption>'
							+'<thead><tr><th>周日</th><th>周一</th><th>周二</th><th>周三</th><th>周四</th><th>周五</th><th>周六</th></tr></thead>'
							+'<tbody>'
							+'<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>'
							+'<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>'
							+'<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>'
							+'<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>'
							+'<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>'
							+'<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>'
							+'</tbody>'
							+'</table>';
				$("#"+opts.dateContainer).append(dbHtml);
				db=$("table.dataBuilder");
				db.addClass(opts.custom_style).attr("id",opts.dbID);
				$.fn.showDate({
					year:opts.year,
					month:opts.month	
				});
				opts.afterDateBuilder();
			}else{
				$.fn.showDate({
					year:opts.year,
					month:opts.month	
				});
				opts.afterDateBuilder();
			}

			// 显示选择日期盒子
			var selectDate='<div class="select_date">'
					+'<div class="year_box">'
						+'<p>选择年份</p>'
						+'<table class="select_y">'
						+'<tr><td></td><td></td></tr>'
						+'<tr><td></td><td></td></tr>'
						+'<tr><td></td><td></td></tr>'
						+'<tr><td></td><td></td></tr>'
						+'<tr><td></td><td></td></tr>'
						+'<tr><td></td><td></td></tr>'
						+'</table>'
						+'<div class="y_btn">'
							+'<span class="before" id="dbYearBefore"><</span><span class="after" id="dbYearAfter">></span></div>'
        				+'</div>'
        			+'<div class="month_box">'
        				+'<p>选择月份</p>'
        				+'<table class="select_m">'
                		+'<tr><td>1</td><td>7</td></tr>'
                		+'<tr><td>2</td><td>8</td></tr>'
                		+'<tr><td>3</td><td>9</td></tr>'
						+'<tr><td>4</td><td>10</td></tr>'
						+'<tr><td>5</td><td>11</td></tr>'
                		+'<tr><td>6</td><td>12</td></tr>'
            			+'</table>'
        			+'</div><br class="clear" />'
        			+'<div class="select_ope"><a href="javascript:void(0);" class="close">关闭</a><a href="javascript:void(0);" class="insure">确定</a></div></div>';
			
			var sDateBox=$("div.select_date");
			
			
			
			if(opts.isBind){
				$.fn.dateBuilder.Defaults.isBind=false;	
				var sd=$("span.date");
				var sdTop=sd.offset().top,sdLeft=sd.offset().left;
				var sYearTable=$("table.select_y"),sMonthTable=$("table.select_m");
				var sYearTableTD=sYearTable.find("td"),sMonthTableTD=sMonthTable.find("td");
				//alert(sdTop+"--"+sdLeft);	
				sd.bind("click",function(){
					var sYear=parseInt($("#dbYear").text());
					var sMonth=parseInt($("#dbMonth").text());
					if(sDateBox.length>0){
						sDateBox.css({
							display:"block",
							left:sdLeft-50+"px",
							top:sdTop+15+"px"	
						});		
					}else{
						$("body").append(selectDate);
						sDateBox=$("div.select_date");
						sDateBox.css({
							display:"block",
							left:sdLeft-50+"px",
							top:sdTop+15+"px"	
						});
					
					}
					
					sYearTable=$("table.select_y");
					sYearTableTD=sYearTable.find("td");	
					sMonthTable=$("table.select_m");
					sMonthTableTD=sMonthTable.find("td");
					
					//选择年月盒子重置数据
					defaultYear(sYear);
					sYearTableTD.eq(0).addClass("select");
					sMonthTableTD.removeClass();
					
					$("div.select_ope").find("a.close").bind("click",function(){
						sDateBox.css("display","none");	
					});
					$("div.select_ope").find("a.insure").bind("click",function(){
						var paraYearTD=sYearTable.find("td.select"),paraMonthTD=sMonthTable.find("td.select");
						var paraYear,paraMonth;
						if(paraYearTD.length>0){
							paraYear=parseInt(paraYearTD.text());	
						}else{
							paraYear=sYear;	
						}
						
						
						if(paraMonthTD.length>0){
							paraMonth=parseInt(paraMonthTD.text())-1;	
						}else{
							paraMonth=sMonth-1;	
						}
						$("#"+opts.dbID).find("td").removeClass();
						$.fn.showDate({
							year:paraYear,
							month:paraMonth,
							afterShowDate:function(){}
						});
						sDateBox.css("display","none");
					});
					
				});
				
				//绑定选择年份前后按钮
				$("#dbYearBefore").live("click",function(){
					var paraYear=parseInt(sYearTableTD.eq(0).text())-12;
					defaultYear(paraYear);
				});	
				$("#dbYearAfter").live("click",function(){
					var paraYear=parseInt(sYearTableTD.eq(0).text())+12;
					defaultYear(paraYear);
				});	
				
				sYearTableTD.live("click",function(){
					sYearTableTD.removeClass();
					$(this).addClass("select");	
				});
				
				sMonthTableTD.live("click",function(){
					sMonthTableTD.removeClass();
					$(this).addClass("select");		
				});
			}
			
			//重置年份表格
			function defaultYear(year){
				var sTDYear=$("table.select_y").find("td");	
				sTDYear.removeClass();
				var col1=year,col2=year+6;
					for(var i=0;i<sTDYear.length;i++)
					{
						if(i%2==0){
							sTDYear.eq(i).text(col1);
							col1++;	
						}else if(i%2!=0){
							sTDYear.eq(i).text(col2);	
							col2++;
						}	
					}
			}
			
			
		}		 
	});	
	
	$.fn.showDate=function(options){
		var opts=$.extend($.fn.showDate.Defaults,options);
		//alert($.fn.showDate.Defaults.year);
		
		var db=$("table.dataBuilder"),dbItem=db.find("td"),dbCYear=$("#dbYear"),dbCMonth=$("#dbMonth");
		
		dateBuildFunc(opts.year,opts.month);
		dbCYear.text(opts.year);
		dbCMonth.text(parseInt(opts.month)+1);
		
		opts.afterShowDate();
		
		//参数年份月份，获得日期排列
		function dateBuildFunc(cYear,cMonth){
			//alert("allow"+"--"+cYear+"--"+cMonth);
			var newDate=new Date(cYear,cMonth,1);
			var firstDayInWeek=newDate.getDay();
			var cTotalDays=getTotalDaysInMonth(opts.year,parseInt(opts.month)); 
			//alert(cTotalDays);
			var itemLen=dbItem.length;
			var curDays=1;
			dbItem.removeClass();
			dbItem.text("");
			for(i=firstDayInWeek;i<itemLen;i++){
				if(curDays>cTotalDays){
					break;	
				}else{
					
					dbItem.eq(i).text(curDays);
					curDays++;
				}	
			}
		}
		
		
		
		//获得某年某月的天数
		function getTotalDaysInMonth(cYear,cMonth){
			var leapYear=false,cTotalDays;
			if(cYear%4==0&&cYear%100!=0){
				leapYear=true;	
			}
			if(cYear%400==0){
				leapYear=true;	
			}
			switch(cMonth){
				case 0:cTotalDays=31;break;
				case 1:cTotalDays=leapYear?29:28;break;
				case 2:cTotalDays=31;break;
				case 3:cTotalDays=30;break;
				case 4:cTotalDays=31;break;
				case 5:cTotalDays=30;break;
				case 6:cTotalDays=31;break;
				case 7:cTotalDays=31;break;
				case 8:cTotalDays=30;break;
				case 9:cTotalDays=31;break;
				case 10:cTotalDays=30;break;
				case 11:cTotalDays=31;break;
			}
			return cTotalDays;
		}	
	};
	
	var curDate=new Date(),curYear=curDate.getFullYear(),curMonth=curDate.getMonth();
	
	$.fn.showDate.Defaults={
		year:curYear,
		month:curMonth,
		afterShowDate:function(){}	
	}
	
	
	
	$.fn.dateBuilder.Defaults={
		year:curYear,    //默认值 当前年份 可传入参数
		month:curMonth,    //默认值 当前月份 可传入参数 0-11
		dbID:"mydb",      //默认值 生成日期表格ID 可传入参数
		dateContainer:"dateBox",   //日期容器ID  必须要有  可传入参数
		custom_style:"custom_style",    //日期客户端样式   可传入参数
		isBind:true,
		afterDateBuilder:function(){}   //date生成以后 回调函数
	}
	

	//重置tabs
	$.defUnit={
		tabsNav:function(){
			var tabsUl=$("ul.tabs_nav");
			var tabsLi=tabsUl.find("li");
			var tabsBox=$("div.tabs_box");
			var tabs=$(".tabs");
			var cIndex=0;
			tabsLi.eq(0).addClass("current");
			tabsLi.bind("mouseover",function(){
				cIndex=$(this).index();
				$(this).addClass("current").siblings().removeClass();
				if(tabsBox.length>0){
					tabs=$(this).parent().parent().siblings("div.tabs");
				}
				
				if(cIndex==0){
					tabs.find("li").css("display","block");
				}else if(cIndex==1){
					tabs.find("li.zc").css({
						display:"block"					   
					}).siblings("li.xx").css({
						display:"none"			
					});	
				}else if(cIndex==2){
					tabs.find("li.xx").css({
						display:"block"					   
					}).siblings("li.zc").css({
						display:"none"			
					});		
				}
				
			});
		},

		contactBox:function(){
			var cb=$("#contact");
			if(cb.length>0){
				var csLink=$("a.contact_sorts");
				//绑定单机展开
				csLink.bind("click",function(){
					if($(this).siblings("ul.contact_child_list").is(":hidden")){
						$(this).siblings("ul.contact_child_list").css("display","block");
						$(this).find("b").attr("class","fold_btn_2");
					}else{
						$(this).siblings("ul.contact_child_list").css("display","none");
						$(this).find("b").attr("class","fold_btn_1");	
					}
						
				});
				var zzBox=$("div.zz_box");
				var cc=$("a.contact_close"),cCancl=$("#c_cancl"),cInsure=$("#c_insure");
				cc.bind("click",function(){
					zzBox=$("div.zz_box");
					cb.css("display","none");
					if(zzBox.is(":visible")){
						zzBox.css("display","none");	
					}
					$("html").css("overflow","auto");	
				});
				cCancl.bind("click",function(){
					removeItem();
				});
				

				
				cInsure.bind("click",function(){
					var allItemsValue="";
					zzBox=$("div.zz_box");
					var acceptBoxItem=$("#accept_item").find("a.name");
					var exsitItems,exsitItemStr=$("#"+$.defUnit.contactBox.Defaults.acceptID).val();
					allItemsValue+=exsitItemStr;
					exsitItems=exsitItemStr.split(";");
					$.each(acceptBoxItem,function(index,value){
						//处理数据项是否存在容器中
						cid=$(value).attr("cid");
						csorts=$(value).attr("csorts");
						cvalue=$(value).text();
						cItem="("+cid+","+csorts+","+cvalue+")";
						var isExsit=false;
						for(var i=0;i<exsitItems.length;i++){
							if(exsitItems[i]==cItem){
								isExsit=true;
								break;	
							}
						}
						if(!isExsit){
							allItemsValue+=cItem+";";
						}
					});	
					//alert($("#"+$.defUnit.contactBox.Defaults.acceptID).val());
					$("#"+$.defUnit.contactBox.Defaults.acceptID).val(allItemsValue);
					removeItem();
					cb.css("display","none");
					if(zzBox.is(":visible")){
						zzBox.css("display","none");	
					}
					$("html").css("overflow","auto");
				});
				
				//绑定单机添加到右边
				var childLink=$("ul.contact_child_list").find("a");
				var acceptBox=$("#accept_item"),acceptUser,aItem="",isContain=false;
				var deleteItemLink=acceptBox.find("span.remove a");
				var cid="",csorts="",cvalue="",cItem="";
				childLink.bind("click",function(){
					checkAndInsert(this);
				});
				
				//检测并添加数据
				function checkAndInsert(curObj){
					cid=$(curObj).attr("cid");
					csorts=$(curObj).attr("csorts");
					cvalue=$(curObj).text();
					cItem="("+cid+","+csorts+","+cvalue+")";
					//alert(cItem);
					//检测是否已经添加
					acceptUser=acceptBox.find("a.name");
					$.each(acceptUser,function(index,user){
						aItem="("+$(user).attr("cid")+","+$(user).attr("csorts")+","+$(user).text()+")";
						if(cItem==aItem){
							isContain=true;
							return false;
						}
					});
					if(!isContain){
						var newItem='<li><span class="remove">[<a href="#">移除</a>]</span>'
						+'<a href="javascript:void(0);" cid="'+cid+'" csorts="'+csorts+'" class="name">'+cvalue+'</a></li>';	
						acceptBox.append(newItem);
					}
					isContain=false;	
				}
				
				
				//移除数据项
				function removeItem(){
					acceptBox.find("li").remove();
				}
				
				
				//绑定删除按钮
				deleteItemLink.live("click",function(){
					$(this).parent().parent().remove();	
				});	
				
				//绑定全部添加
				var allItemLink=$("a.contact_sorts_nums");
				allItemLink.bind("click",function(){
					var allChildItem=$(this).siblings("ul.contact_child_list").find("a");
					$.each(allChildItem,function(index,value){
						checkAndInsert(value);
					});	
				});
				
			}	
		},
		popDiv:function(options){
			var opts=$.extend({},$.defUnit.popDiv.Defaults,options);
			var tcBox=$("#"+opts.tcID),tcWidth=tcBox.width(),tcHeight=tcBox.height();
			var zzHtml="<div class='zz_box'><div>";
			var winWidth=$(window).width(),winHeight=$(window).height();
			if(opts.isZZ){
				var zzBox=$("div.zz_box");
				if(zzBox.length>0){
					zzBox.css({
						display:"block"
					});
				}else{
					$("body").append(zzHtml);
					zzBox=$("div.zz_box");
					zzBox.css({
						display:"block",
						width:winWidth+"px",
						height:winHeight+"px"	
					});		
				}
				$("html").css("overflow","hidden");		

			}
			tcBox.css({
				position:"absolute",
				zIndex:9999,
				left:(winWidth-tcWidth)/2+"px",
				top:(winHeight-tcHeight)/2+"px",
				display:"block"	
			});	
		},
		
		pager:function(options){
			var opts=$.extend({},$.defUnit.pager.Defaults,options);
			var html='<div class="fanye">'
                    	+'<a href="javascript:void(0);" id="prev_page">上一页</a><a href="javascript:void(0);" id="next_page">下一页</a>'
						+'<span class="total_page" id="showPage">'+opts.curPage+'/'+opts.totalPage+'</span>'
						+'<span id="pageContainer"></span>'
                    	+'</div>';
			var container=$("#"+opts.containerID);
			container.append(html).addClass(opts.customStyle);
			var prevPage=$("#prev_page");
			var nextPage=$("#next_page");
			var conShowPage=$("#showPage");
			var conPage=$("#pageContainer");
			
			showPage(opts.curPage);
			//opts.afterClickPage(opts.curPage);
			
			
			function showPage(paraCurPage){
				conPage.html(" ");
				//控制上一页与下一页按钮
				if(opts.totalPage==1){
					prevPage.css("display","none");	
					nextPage.css("display","none");	
				}else if(opts.totalPage==paraCurPage){
					nextPage.css("display","none");	
				}else if(paraCurPage==1){
					prevPage.css("display","none");		
				}else{
					prevPage.css("display","inline-block");	
					nextPage.css("display","inline-block");		
				}
				//控制页码显示方式
				if(opts.totalPage<=21){
					var pageLink="";
					for(var i=1;i<=opts.totalPage;i++){
						if(i==paraCurPage){
							pageLink+='<a href="javascript:void(0);" class="current">'+i+'</a>';	
						}else{
							pageLink+='<a href="javascript:void(0);">'+i+'</a>';	
						}
						
					}
					conPage.append(pageLink);	
				}
				
				if(opts.totalPage>21){
					if(paraCurPage<=11){
						var pageLink="";
						for(var i=1; i<=paraCurPage-1;i++){
							pageLink+='<a href="javascript:void(0);">'+i+'</a>';
						}
						pageLink+='<a href="javascript:void(0);" class="current">'+paraCurPage+'</a>';
						for(var i=paraCurPage+1;i<=21;i++){
							if(i==20){
								pageLink+='<span>...</span>';		
							}else if(i==21){
								pageLink+='<a href="javascript:void(0);">'+opts.totalPage+'</a>';	
							}else{
								pageLink+='<a href="javascript:void(0);">'+i+'</a>';		
							}	
						}					
	
						conPage.append(pageLink);		
					}
					//开头
					if(paraCurPage>11&&paraCurPage<=opts.totalPage-9){
						var prevAllLink="",nextAllLink="",linkItem="",curPageLink="",allLink="";
						var p=parseInt(paraCurPage),n=parseInt(paraCurPage);
						for(var j=1;j<=10;j++){
							p--;
							if(j==9){
								linkItem='<span>...</span>';	
							}else if(j==10){
								linkItem='<a href="javascript:void(0);">1</a>';		
							}else{
								linkItem='<a href="javascript:void(0);">'+p+'</a>';		
							}
							prevAllLink=linkItem+prevAllLink;
						}
						curPageLink='<a href="javascript:void(0);" class="current">'+paraCurPage+'</a>';	
						for(var j=1;j<=10;j++){
							n++;
							if(j==9){
								linkItem='<span>...</span>';	
							}else if(j==10){
								linkItem='<a href="javascript:void(0);">'+opts.totalPage+'</a>';			
							}else{
								linkItem='<a href="javascript:void(0);">'+n+'</a>';		
							}
							nextAllLink=nextAllLink+linkItem;
						}
						allLink=prevAllLink+curPageLink+nextAllLink;
						conPage.append(allLink);		
					}
					
					//末尾
					if(paraCurPage>(opts.totalPage-9)){
						var prevAllLink="",nextAllLink="",linkItem="",curPageLink="",allLink="";
						var p=parseInt(paraCurPage),n=parseInt(paraCurPage);
						var afterLoopTimes=opts.totalPage-paraCurPage;
						var beforeLoopTimes=20-afterLoopTimes;
						for(var j=1;j<=beforeLoopTimes;j++){
							p--;
							if(j==beforeLoopTimes-1){
								linkItem='<span>...</span>';	
							}else if(j==beforeLoopTimes){
								linkItem='<a href="javascript:void(0);">1</a>';		
							}else{
								linkItem='<a href="javascript:void(0);">'+p+'</a>';		
							}
							prevAllLink=linkItem+prevAllLink;
						}
						curPageLink='<a href="javascript:void(0);" class="current">'+paraCurPage+'</a>';
							
						for(var j=1;j<=afterLoopTimes;j++){
							n++;
							linkItem='<a href="javascript:void(0);">'+n+'</a>';		
							nextAllLink=nextAllLink+linkItem;
						}
						allLink=prevAllLink+curPageLink+nextAllLink;
						conPage.append(allLink);	
					}
					
						
				}
				
				//空值当前页/总页
				conShowPage.text(paraCurPage+"/"+opts.totalPage);
			}
			
			//绑定数据
			conPage.find("a").live("click",function(){
				var pageText=parseInt($(this).text());
				//alert(pageText);
				$(this).addClass("current").siblings().removeClass();
				
				showPage(pageText);
				opts.afterClickPage(pageText);
			});
			
			prevPage.bind("click",function(){
				var pageText=parseInt(conPage.find("a.current").text())-1;
				showPage(pageText);
				opts.afterClickPage(pageText);
			});
			
			nextPage.bind("click",function(){
				var pageText=parseInt(conPage.find("a.current").text())+1;
				showPage(pageText);
				opts.afterClickPage(pageText);
			});
			
			
		}
		
		
			
	};
	
	
	$.defUnit.pager.Defaults={
		curPage:1,
		totalPage:30,
		customStyle:"customStyle",
		containerID:"fy_container",
		afterClickPage:function(){}	
	}
	
	$.defUnit.popDiv.Defaults={
		tcID:"tcID", //需先进入body
		zzBox:"zz_box",
		isZZ:true	
	}
	
	$.defUnit.contactBox.Defaults={
		acceptID:"acceptID" //默认接受contact容器
	}
	
	$(function(){
		$.defUnit.tabsNav();	   
	});
	
})(jQuery);

